package com.cnunicom.modules.revival.service.impl;

import com.cnunicom.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnunicom.common.utils.PageUtils;
import com.cnunicom.common.utils.Query;

import com.cnunicom.modules.revival.dao.RevivalProvinceDao;
import com.cnunicom.modules.revival.entity.RevivalProvinceEntity;
import com.cnunicom.modules.revival.service.RevivalProvinceService;


@Service("revivalProvinceService")
public class RevivalProvinceServiceImpl extends ServiceImpl<RevivalProvinceDao, RevivalProvinceEntity> implements RevivalProvinceService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) throws ParseException {

        RevivalProvinceEntity entity = getRevivalProvinceEntity(params);

        IPage<RevivalProvinceEntity> page = this.page(
                new Query<RevivalProvinceEntity>().getPage(params),
                new QueryWrapper<RevivalProvinceEntity>()
                        .setEntity(entity)
                        .between(StringUtils.isNotBlank(entity.getUpdateStartTime()),"update_time",entity.getUpdateStartTime(),entity.getUpdateEndTime())
        );

        return new PageUtils(page);
    }


    /**
     * page查询参数处理
     * @param params
     * @return
     * @throws ParseException
     */
    private RevivalProvinceEntity getRevivalProvinceEntity(Map<String, Object> params) throws ParseException {

        RevivalProvinceEntity entity = new RevivalProvinceEntity();

        boolean isAuthor = SecurityUtils.getSubject().hasRole("作者");
        SysUserEntity user = (SysUserEntity) params.get("user");
        Long userId = user.getUserId();


        String articleTitle = (String) params.get("articleTitle");
        String updateTime = (String) params.get("updateTime");
        String articleType = (String) params.get("articleType");
        String username = (String) params.get("createUserName");


        if (isAuthor) {
           entity.setCreateUserId(userId);
        } else if (StringUtils.isNotBlank(articleType)) {
            entity.setArticleType(Integer.parseInt(articleType));
        } else if (StringUtils.isNotBlank(username)) {
            entity.setCreateUserName(username);
        } else if (StringUtils.isNotBlank(articleTitle)) {
            entity.setArticleTitle(articleTitle);
        } else if (StringUtils.isNotBlank(updateTime)) {
            String[] updateTimeSplit = updateTime.split(",");
            entity.setUpdateStartTime(updateTimeSplit[0]);
            entity.setUpdateEndTime(updateTimeSplit[1].concat(" 23:59:59"));
        }

        return entity;
    }


}
